Sigo recibiendo estos 2 errores, pase lo que pase: Error: data and hash arguments required y Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Recibo el primer error al comparar hash y texto sin formato. Recibo el segundo error en el archivo fetchUser.js cuando intento enviar el token jwt en los encabezados de solicitud
Aquí está mi ruta de inicio de sesión de userAuth:
router.post('/login', checks.loginChecks, async (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } const { email, password } = req.body; try { let thatOneUser = await User.find({ email }); console.log(thatOneUser); if (!thatOneUser) return res.status(403).json({ success: false, message: `Please enter the correct information!`, }); bcrypt.compare(password, thatOneUser.password, (err, result) => { if (!result) { console.log(err); return res.status(403).json({ success: false, message: `Please enter the correct information!`, err, }); } }); const authData = { id: thatOneUser.id }; const authToken = jwt.sign(authData, JWT_SECRET); res.json({ success: true, message: 'User has been logged!', authToken }); } catch (error) { res.status(500).json({ success: false, message: 'Error occured during logging the user', error, }); console.log(error); } });y aquí está mi fetchUser.js:
const jwt = require('jsonwebtoken'); const JWT_SECRET = 'jwtsecretabc123'; const fetchUser = (req, res, next) => { // Get the user from the jwt token and add id to req object const token = req.header('auth-token'); console.log(token); if (!token) return res.status(401).json({ success: false, message: 'Please authenticate using a valid token!', }); try { const decoded = jwt.verify(token, JWT_SECRET); req.user = decoded.user; console.log(decoded.user, ' I am decoded id!'); next(); } catch (error) { return res .status(401) .json({ success: false, message: 'failed to decode the token' }); } }; module.exports = fetchUser; cuando hago una solicitud a localhost:5000/api/auth/login , funciona la primera vez, pero no funciona la segunda vez.
Ninguna de las otras solicitudes (registro, getUserData) tampoco funciona. ¡Cualquier ayuda será apreciada!